﻿using Microsoft.AspNetCore.Mvc;

namespace SseWebApi.Controllers
{
    [ApiController]
    public class HomeController : Controller
    {
        [Route("data")]
        [HttpGet]
        public IActionResult Data()
        {
            Random random = new Random();
            string _event = "message";
            string data = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            if (random.Next(0, 10) % 3 == 0)
            {
                data = "新消息";
                // _event = "NewMsg";      //切换监听事件
            }

            Response.ContentType = "text/event-stream";
            return Content($"retry:{1000}\nevent:{_event}\nid:{DateTime.Now.Ticks}\ndata:{data}\n\n");
        }
    }
}
